package com.datechange.demo.controller;

import cn.hutool.core.io.FileUtil;

import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Test {
    public static void main(String[] args) throws Exception {

//        URL url = new URL("http://10.9.3.206:10039/cms/tools/downloadfile?fileId=b6ad9540d98011e79b2a84e84eec5bcf");
//        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
//        conn.setConnectTimeout(3 * 1000);
//        conn.setRequestProperty("cookie", "JSESSIONID=0000w6MNld-GO52DgfjDGL-y0-C:-1;LtpaToken2=DJTPBWWThHvXaUL+1e++Qq2SVx7eNiGBvjZgRp+LkuNSVheT5Ee8H/Y+Lsb3dmvNppWf4LLRjYqmmjKcfC6PrnzXPOhf7ohqM8TajB0pxNEYyXfsuIy3fcBqjQcs4H0Th3vHSS85nuGnmQDiHPLBzfpHk/vMbQ6+394CXxOY4zeOJKTT+a5K9J18tfTBrkS3MBGpSojjIi1xt0SQLV6u9Ho8gdFA2HkST8+MHxrrNq93Iui5nRdd5RJATObe0MICxMV28MNf314MQMLBrYQvUuF6pNsFynVpyHawgzeblgOeb24guxA7txhO3bHh7YUPrjlZlOooIyboxbLeceq+QBacP/QPt4gKXKpAf2xWZNtu1wY1cVkhRyN0zHJ8iSTRzMvMw1wJHS7VJ1SDZ3h/P8HAEZYwC0WypARBC/rh26qCo8joTUijwyehL9b84ZP5PYzqoD60Xn632X/FE0ojG4terQMKt5yS5v68NUepcBr7KBfUiPyqQXeLQs/OYiSqyThE1pMmEmvjMFpg9Z6WahGH84YckZu/sTrTr/l7JQcqA8BfK/cm4wO6fSR/ByyI+Y6jjbSKsxvUPGdea7lymQqu3P70hvrDMR5hdZTWz9FAz1kVvhRe/vOC/tbyd2e+0yt3q3Lqm0aWsv2x9u+AmVvg2KdS0wUax1p9WGhERyq87HYencNyz7bqD48kt768/KZ8QEj5ZA7rrhihRSJFldPBBjSaz0j2KHuYeWZHNKj2iSu5kFT2dfH4X826HNuF");
//        //防止屏蔽程序抓取而放回403错误
//        conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:83.0) Gecko/20100101 Firefox/83.0");
//        conn.setRequestProperty("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
//        conn.setRequestProperty("Accept-Encoding","gzip, deflate");
//        conn.setRequestProperty("Connection","keep-alive");
//        conn.setRequestProperty("Host","10.9.3.206:10039");
//        conn.setRequestProperty("Upgrade-Insecure-Requests","1");
//        conn.setRequestProperty("Referer","http://10.9.3.206:10039/wps/myportal/tj/grportal/contentView?cpcid=840c2840d98011e79b2a84e84eec5bcf");
//       Long totalSize = Long.parseLong(conn.getHeaderField("Content-Length"));
//        FileUtils.copyURLToFile(url, new File("D:\\1122131333.xls"));

//        downLoadJpg("","");


//        String s = "http://10.9.3.206:10039/cms/CmsPc/getUploadImg?imgName=/%E6%96%B0%E9%97%BB%E5%9B%BE%E4%BA%94%E4%BA%BA.jpg";
//        String[] split = s.split("imgName=/");
//        System.out.println(split[0]);
//        System.out.println(split[1]);
//        String decode = URLDecoder.decode(split[1],"UTF-8");
//        String sss = s.replace("http://10.9.3.206:10039/cms/CmsPc/getUploadImg?imgName=/", "www.baidu.com");
//        String replace = sss.replace(split[1], decode);
//        System.out.println(replace);

        String img = "";
        Pattern p_image;
        Matcher m_image;
        List<String> pics = new ArrayList<String>();
        String htmlStr = "<img style=\"HEIGHT: 410px; WIDTH: 600px\" alt=\"\" src=\"http://10.9.3.206:10039/cms/CmsPc/getUploadImg?imgName=/%E6%96%B0%E9%97%BB%E5%9B%BE%E4%BA%94%E4%BA%BA.jpg\" />sdfhjkwehiufnweuifniwefne" +
                "<img style=\"HEIGHT: 410px; WIDTH: 600px\" alt=\"\" src=\"http://10.9.3.206:10039/cms/CmsPc/getUploadImg?imgName=/%E6%96%B0%E9%97%BB%E5%9B%BE%E4%BA%94%E4%BA%BA.1111jpg\">";

        String regEx_img = "<img.*src\\s*=\\s*(.*?)[^>]*?>";
        p_image = Pattern.compile(regEx_img, Pattern.CASE_INSENSITIVE);
        m_image = p_image.matcher(htmlStr);
        while (m_image.find()) {
            img = img + "," + m_image.group();
            // Matcher m =
            // Pattern.compile("src=\"?(.*?)(\"|>|\\s+)").matcher(img); //匹配src
            Matcher m = Pattern.compile("src\\s*=\\s*\"?(.*?)(\"|>|\\s+)").matcher(img);
            while (m.find()) {
                pics.add(m.group(1));
            }
        }
        for (String pic : pics) {
            System.out.println(pic);
            //字符串切割
            String[] split = pic.split("imgName=/");
            //将'imgName=/'之后的内容解码
            String decode = URLDecoder.decode(split[1],"UTF-8");
            System.out.println(split[1]);
            //先替换地址
            htmlStr=htmlStr.replace("http://10.9.3.206:10039/cms/CmsPc/getUploadImg?imgName=/","http://test.com")
            .replace(split[1],decode);
        }
        System.out.println(htmlStr);
    }

    public static String downLoadJpg(String fileId) {
        try {
            String webURL="http://10.9.3.206:10039/cms/tools/downloadfile?fileId="+fileId;
            //网络URL
            URL url = new URL(webURL);
            //打开网络连接
            URLConnection connection = url.openConnection();
            connection.setRequestProperty("cookie", "JSESSIONID=0000CI-DT2_KKLbZWqdfmFjMm6o:-1; LtpaToken2=wPd8PDMNENlVX3SMoiPorhqh+5pJkLByoK+QwaYaAc9qCLk9reu7E3aqOmxIbhVwqnXTZGvRyYcRy59ThIyu7xCFU38WW1FNw36N3gMFMeLP+e0/V05oS2fOaCtFg5nQTACaNHCEkFczflPgy6G01pPFw5pnTxd2z5qOvHflVY0TL8z7WFWbEST928TpMpPjFzA9hm9892yVyK/k0ryjD3iax4wrvpBC9vze+t79Ybl4NMd7f3hAyw5G/IQA9OikwXeMuduhDEuZWvmLpaSKYm1cZpJ69r2xIL1mvImdo9K8NVkr/tXvmUk6UIt4lmFh1QOPSTm2OfKLZUwNXi/6br0/bpFktr9bc6pjPB5bwQCjk6xgy4IHNw79AzssMavDoXvtZT7Ri2krURGYDcVFHu7kDiiuioFSTl0OI7U6k9IPy2vzKO8Dcj11UxrygEtXGvzVhGCMPe3YrT/yz16j9h4AHZZABeV2RP+fxvV8u1qcDb56/Xy6Hlg2jJT5pQEPfPMOcstu2rButenE62TpvJcz2n0Yw7u8pVBildzxkz6Qsqxg/ssb5R7r1t0ePY0RAQKYY/wNvDOuIxNa2NosAnJH3WRtlUlH5R6w4zPTAddEWCHRbnZp34bpF+xdjE8Z/KjovYO2RflSzIm4xtGMUdlbWlx3X8q+FtC+X5QgXlhkcEOGzsRmxbFzhRB7/SaEND65Cmbe+Iy79Ye92Z35DoUHD8lSHKp158a4uXMBDz2eSFolZN0neguIzYQQbdGV");
            //防止屏蔽程序抓取而放回403错误
            connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:83.0) Gecko/20100101 Firefox/83.0");
            connection.setRequestProperty("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
            connection.setRequestProperty("Accept-Encoding","gzip, deflate");
            connection.setRequestProperty("Connection","keep-alive");
            connection.setRequestProperty("Host","10.9.3.206:10039");
            connection.setRequestProperty("Upgrade-Insecure-Requests","1");
            connection.setRequestProperty("Referer","http://10.9.3.206:10039/wps/myportal/tj/grportal/contentView?cpcid=840c2840d98011e79b2a84e84eec5bcf");
            //输入流
            InputStream inputStream = connection.getInputStream();
            String contentDisposition = new String(connection.getHeaderField("content-Disposition").getBytes("ISO-8859-1"), "GBK");
            System.out.println(contentDisposition);
            // 匹配文件名
            String fileName =contentDisposition.replaceFirst("(?i)^.*filename=\"?([^\"]+)\"?.*$", "$1");
            System.out.println(fileName);
            String filePath="D:\\test\\"+fileName;
            FileUtil.writeFromStream(inputStream, filePath);
            return filePath;
        } catch (Exception e) {
            //监听下载失败
            e.printStackTrace();
        }
        return null;
    }
}
